AUXFILES  := Makefile
PROJDIRS  := inc src
CSRC      := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.c")
ASMSRC    := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.asm")
HDRFILES  := $(shell find $(PROJDIRS) -mindepth 1 -maxdepth 3 -name "*.h")
COBJ      := $(patsubst %.c,%.o,$(CSRC))
ASMOBJ    := $(patsubst %.asm,%.asmo,$(ASMSRC))
DEPFILES  := $(patsubst %.c,%.d,$(CSRC))
ALLFILES  := $(CSRC) $(ASMSRC) $(HDRFILES) $(AUXFILES)
CFLAGS    := -O -Wall -Wextra -Werror -std=c99 -pedantic \
				 -nostdinc -fno-builtin -ffreestanding -fno-stack-protector \
				 -Wshadow -Wpointer-arith -Wcast-align \
				 -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs \
				 -Winline -Wno-long-long -Wconversion -Wstrict-prototypes \
				 
_PATH_    := ../toolchain/cross
COMPILER  := $(_PATH_)/bin/i586-pc-fluidium-gcc
LINKER    := $(_PATH_)/bin/i586-pc-fluidium-ld
ASSEMBLER := nasm

.PHONY: all clean dist todo

all: kernel

kernel: $(ASMOBJ) $(COBJ) 
	@$(LINKER) -T link.ld -o package/packages/kernel/kernel $? 

clean:
	-@$(RM) $(wildcard $(COBJ) $(ASMOBJ) $(DEPFILES) fluidium.pkg.tgz)

dist:
	@tar czf fluidium.pkg.tgz --exclude ".svn" --group=root --owner=root -C package .

todo:
	-@for file in $(CSRC); do fgrep -H -e TODO -e FIXME $$file; done; true

-include $(DEPFILES)

%.o: %.c Makefile
	@$(COMPILER) $(CFLAGS) -MMD -MP -MT "$*.d" -g -I./inc -c $< -o $@

%.asmo: %.asm Makefile
	@$(ASSEMBLER) -f elf -o $@ $< 

